
const app = getApp();
Page({
  data: {
    title: '相关条款',
    name: '',
    phoneNumber: '',
    identityCard: '',
    bankName: '',
    bankId:'',
    isdisabled:false,
    loading:false,
    banklist: []
  },
  onShow:function(){
    const _this = this;
    wx.getStorage({
      key: 'banks',
      success: function (res) {
        _this.setData({
          bankName: res.data.bankName,
          bankId: res.data.bankId,
        })
        console.log(res.data);
      }
    })
    wx.removeStorage({
      key: 'banks',
      success: function (res) {
        console.log(res.data)
      }
    })
  },
  onLoad: function (options) {
    // console.log(options.bankName);
    if(options.bankName){
      this.setData({
        bankName:options.bankName
      })
    }
    this.getAgreeMent();
    this.getInfo();
    wx.getStorage({
      key: 'changecard',
      success: function (res) {
        _this.setData({
          cardNumber: res.data.cardNumber
        })
        console.log(res.data);
      }
    })
  },
  //是否同意
  // checkboxChange: function (e) {
  //   if (e.detail.value.length == 0) {
  //     this.setData({
  //       check: 0  //未选
  //     })
  //   }
  //   else {
  //     this.setData({
  //       check: 1  //已选
  //     })
  //   }
  //   // console.log(this.data.check)
  // },
  //用户信息
  getInfo() {
    const _this = this;
    let baseInfo = app.globalData.baseInfo;
    _this.setData({
      name: baseInfo.name,
      phoneNumber: baseInfo.phoneNumber,
      identityCard: baseInfo.identityCard,
    })
  },
  // 条款
  getAgreeMent: function () {
    this.setData({
      title: app.globalData.agreementTitle
    })
  },
  // 自动匹配银行卡
  getBankNumber: function (e) {
    // console.log(e.detail.value);
    const _this = this;
    _this.setData({
      cardNumber: e.detail.value
    })
    wx.request({
      url: app.globalData.path + 'services/app/applets/GetBankShortName',
      data: {
        _input_charset: 'utf-8',
        cardNo: e.detail.value
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.success) {
          if (res.data.result.validated) {
                _this.setData({
                  id: res.data.result.bank,
                  isdisabled: false
                })
           _this.getBankList(_this.data.id);
          } else {
            _this.setData({
              id: ''
            })
          }
          if (!res.data.result.bank) {
            console.log("不存在")
            wx.showToast({
              title: '暂不支持该行，请更换银行卡！',
              icon: 'none',
              duration: 2000
            })
            _this.setData({
              isdisabled: true
            })
          }
        } else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      },
      complete: function (res) {
        // wx.showToast({
        //   title: '1111111111111111111111111',
        //   icon: 'success',
        //   duration: 2000
        // })
      }
    })
  },
  // 银行卡信息获得
  getBankList: function (id) {
    const _this = this;
    wx.request({
      url: app.globalData.path + 'services/app/applets/GetBankList',
      data: {
        bankCode: id
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
            if (res.data.result.items.length > 1) {
              _this.setData({
                bankName: '',
                bankId: '',
                banklist: res.data.result.items
              })
            } else if (res.data.result.items.length == 1) {
              _this.setData({
                bankName: res.data.result.items[0].bankName,
                bankId: res.data.result.items[0].id
              })
            } else if (res.data.result.items.length == 0){
            wx.showToast({
              title: '暂不支持该行，请更换银行卡！',
              icon: 'none',
              duration: 2000
            })
            _this.setData({
              isdisabled: true
            })
          }
        } else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 下一步
  formSubmit: function (e) {
    const _this = this;
    console.log(e);
    console.log(_this.data.bankId)
    if (e.detail.value.cardNumber == '' || _this.data.bankId == ''){
      wx.showModal({
        content: '请将信息填写完整',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            // console.log('用户点击确定')
          }
        }
      });
    }else{
      wx.removeStorage({
        key: 'changecard',
        success: function (res) {
          console.log(res.data)
        }
      })
      _this.setData({
        loading:true,
        isdisabled:true
      })
      wx.request({
        url: app.globalData.path + 'services/app/applets/ChangeBankCard',
        data: {
          bankCode: _this.data.bankId,
          bankCard: e.detail.value.cardNumber
        },
        method: 'POST',
        header: {
          'content-type': 'application/json', // 默认值
          'Authorization': 'Bearer ' + app.globalData.authorization
        },
        success: function (res) {
          console.log(res.data)
          if (res.data.success) {
            _this.setData({
              loading: false,
              isdisabled: false
            })
            wx.navigateTo({
              url: '../my/bank/bank'
            })
          } else {
            _this.setData({
              loading: false,
              isdisabled: false
            })
            if (res.data.error.code == -1) {
              wx.showToast({
                title: '请添加与员工信息一致的银行卡',
                icon: 'none',
                duration: 2000
              })
            }else{
              wx.showModal({
                content: res.data.error.message,
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  }
                }
              });
            }
          }
        }
      })
    }
  },
  toBanks: function () {
    const _this = this;
    wx.setStorage({
      key: 'changecard',
      data: {
        cardNumber: _this.data.cardNumber
      }
    })
    wx.navigateTo({
      url: '../banks/banks?from=change',
    })
  },
  // 协议
  // agreeMent: function () {
  //   wx.navigateTo({
  //     url: '../agreement/agreement',
  //   })
  // }
})